using System;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.CrashReport;
using Voodoo.Sauce.Internal.Utils;

namespace Voodoo.Sauce.Internal.Analytics
{
	public class AudioAdsAnalyticsManager : IAudioAdsAnalyticsManager
	{
		private AudioAdAnalyticsInfo _currentImpressionInfo;

		private string _currentNetworkName;

		public event Action<AudioAdTriggerAnalyticsInfo> OnAudioAdTriggerEvent;

		public event Action<AudioAdAnalyticsInfo> OnAudioAdShownEvent;

		public event Action<AudioAdImpressionAnalyticsInfo> OnAudioAdImpressionEvent;

		public event Action<AudioAdAnalyticsInfo> OnAudioAdClickEvent;

		public event Action<AudioAdAnalyticsInfo> OnAudioAdWatchedEvent;

		public event Action<AudioAdAnalyticsInfo> OnAudioAdCloseEvent;

		public event Action<AudioAdFailedAnalyticsInfo> OnAudioAdFailedEvent;

		private void SetAudioAdInfoCommonParameters(AudioAdAnalyticsInfo info)
		{
			info.gameCount = AnalyticsStorageHelper.Instance.GetGameCount();
			info.audioAdType = "game_start";
		}

		public void TrackAudioAdTrigger(string networkName, bool adLoaded)
		{
			TryPrepareNewImpressionData();
			AudioAdTriggerAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdTriggerAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			info.AdLoaded = adLoaded;
			CallOnMainThread(delegate
			{
				this.OnAudioAdTriggerEvent?.Invoke(info);
			});
		}

		public void TrackAudioAdShown(string networkName)
		{
			TryPrepareNewImpressionData();
			AudioAdAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			CallOnMainThread(delegate
			{
				this.OnAudioAdShownEvent?.Invoke(info);
				VoodooSauceCore.GetCrashReport().LogAdEvent(AdEventParams.AdEventName.audio_ad_shown.ToString(), new AdEventParams(info.networkName, info.audioAdType, "audio_ad", info.impressionId));
			});
		}

		public void TrackAudioAdImpression(string networkName, double revenue)
		{
			TryPrepareNewImpressionData();
			AudioAdImpressionAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdImpressionAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			info.adUnitFormat = "Logo";
			info.revenue = revenue;
			CallOnMainThread(delegate
			{
				this.OnAudioAdImpressionEvent?.Invoke(info);
			});
		}

		public void TrackAudioAdClicked(string networkName)
		{
			TryPrepareNewImpressionData();
			AudioAdAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			CallOnMainThread(delegate
			{
				this.OnAudioAdClickEvent?.Invoke(info);
			});
		}

		public void TrackAudioAdWatched(string networkName)
		{
			TryPrepareNewImpressionData();
			AudioAdAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			CallOnMainThread(delegate
			{
				this.OnAudioAdWatchedEvent?.Invoke(info);
			});
		}

		public void TrackAudioAdClosed(string networkName)
		{
			TryPrepareNewImpressionData();
			AudioAdAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			CallOnMainThread(delegate
			{
				this.OnAudioAdCloseEvent?.Invoke(info);
			});
		}

		public void TrackAudioAdFailed(string networkName, string errorCode)
		{
			TryPrepareNewImpressionData();
			AudioAdFailedAnalyticsInfo info = _currentImpressionInfo.Copy<AudioAdFailedAnalyticsInfo>();
			SetAudioAdInfoCommonParameters(info);
			info.networkName = networkName;
			info.errorCode = errorCode;
			CallOnMainThread(delegate
			{
				this.OnAudioAdFailedEvent?.Invoke(info);
			});
		}

		public void PrepareNewImpressionData()
		{
			_currentImpressionInfo = AudioAdAnalyticsInfo.Create<AudioAdAnalyticsInfo>();
		}

		private void TryPrepareNewImpressionData()
		{
			if (_currentImpressionInfo == null)
			{
				_currentImpressionInfo = AudioAdAnalyticsInfo.Create<AudioAdAnalyticsInfo>();
			}
		}

		private void CallOnMainThread(Action callback)
		{
			UnityThreadExecutor.Execute(callback);
		}
	}
}
